package com.hnkywl.sts.dao.sys;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.sys.Khjwh;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 说明：
 * 包:        com.hnkywl.sts.dao.sys
 * 类描述:    控货价dao类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Repository
public class KhjwhDao extends SimpleHibernateTemplate<Khjwh, Long> {

    public Page<Khjwh> findAllKhjwhList(String pmName, Date start, Date endDate, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Khjwh> page = new Page<Khjwh>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (start != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" dateCreated >= ? ");
            param.add(start);
        }
        if (endDate != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" dateCreated < ? ");
            param.add(endDate);
        }
        if (pmName != null && !pmName.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" pm.name like ? ");
            param.add("%" + pmName.trim() + "%");
        }
        //==============记录数量===============
        hql = "select count(*) from Khjwh where deleted != 1  " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Khjwh where deleted != 1 " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        hql = hql + " order by drrq desc ";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public Khjwh findKhjByPmId(Long pmId) {
        DateFormat format = new SimpleDateFormat("yyyyMMdd");
        String hql = " from Khjwh where (to_char(drrq,'yyyyMMdd') = ? or to_char(last_updated,'yyyyMMdd') = ?) and pm.id = ?" +
                   "  order by last_updated desc ";
        List<Khjwh> khjwhList = this.find(hql, format.format(new Date()), format.format(new Date()), pmId);
        return CollectionUtils.isEmpty(khjwhList) ? null : khjwhList.get(0);
    }
}
